package com.tongxin.van.frameworkcanary.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

/**
 *
 * author: VanyLiu
 * email: 13212674795@163.com
 * description:QQ分享工具类
 *
**/

public class QQUtils {

    public static final String APP_ID = "1106807284";

    /**
     * 分享图片
     */
    public static void shareImage(Activity activity, String imagePath){
        Tencent mTencent = Tencent.createInstance(APP_ID, activity.getApplicationContext());
        Bundle params = new Bundle();
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL,imagePath);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "亮果国际");
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
        //加上这句会分享到QQ空间，不加是分享到QQ好友
        //params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        mTencent.shareToQQ(activity, params, new BaseUiListener(activity));
    }

    /**
     * 分享网页
     */
    public static void shareWebPage(Activity activity, String title, String description, String imageUrl, String webUrl){
        Tencent mTencent = Tencent.createInstance(APP_ID, activity.getApplicationContext());
        Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  description);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageUrl);
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  webUrl);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME,  "亮果国际");
        mTencent.shareToQQ(activity, params, new BaseUiListener(activity));
    }

    /**
     * Ui监听，QQ分享必须
     */
    public static class BaseUiListener implements IUiListener {
        Context context;
        public BaseUiListener(Context context) {
            this.context = context;
        }

        @Override
        public void onComplete(Object response) {
            JSONObject jsonObj = (JSONObject) response;
            Toast.makeText(context, "已分享到QQ", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(UiError e) {
            Toast.makeText(context, "分享出错:"+e.errorCode, Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onCancel() {
            Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
        }
    }
}

